package com.example.testnews.fragmant;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.testnews.R;
import com.example.testnews.adapter.NewsAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private View view;
    private TabLayout tabs;
    private ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        initView();
        return view;
    }

    private void initView() {
        tabs = view.findViewById(R.id.tabs);
        viewPager = view.findViewById(R.id.viewPager);


        NewsFragment sportsNewsFragment = new NewsFragment();
        NewsFragment foodNewsFragment = new NewsFragment();
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(sportsNewsFragment);
        fragments.add(foodNewsFragment);

        String sportsName = "体育";
        String foodName = "美食";
        List<String> names = new ArrayList<>();
        names.add(sportsName);
        names.add(foodName);

        NewsAdapter newsAdapter = new NewsAdapter(getChildFragmentManager(),0,fragments,names);

        viewPager.setAdapter(newsAdapter);
        tabs.setupWithViewPager(viewPager);


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
